package gnss

/*
GPS 时间选取
收到后进行InputGpsT, false表示now过了当前统计时间段，需要进行reset
*/
type GpsTimeSel struct {
	t0      int64         // 统计时间段
	gpstMap map[int64]int // gps时间统计值
	msspan  int64
}

func NewGpsTimeSel() *GpsTimeSel {
	rval := &GpsTimeSel{gpstMap: make(map[int64]int), msspan: 1000}
	return rval
}

func (this *GpsTimeSel) Reset() {
	for k := range this.gpstMap {
		delete(this.gpstMap, k)
	}
	this.t0 = 0
}

func (this *GpsTimeSel) GetCurrentGpsT(minCount int) (t0 int64, cnt int, gpst int64) {
	var m int
	for k, n := range this.gpstMap {
		if n < minCount {
			continue
		}
		if n > m {
			gpst = k
			m = n
		}
	}
	return this.t0, m, gpst
}

func (this *GpsTimeSel) InputGpsT(nowUnixMs int64, gpst int64) bool {
	msspan := this.msspan
	if msspan <= 0 {
		msspan = 1000
	}

	unixMs := nowUnixMs
	unixMs = unixMs - (unixMs % msspan)
	if this.t0 == 0 {
		this.t0 = unixMs
	}
	if unixMs == this.t0 {
		this.gpstMap[gpst]++
		return true
	}
	return false
}
